package structures.search;

import java.util.Arrays;

// 线性查找
public class SeqSearch {

    private static final int LENGTH = 20;
    private static final int VALUE = 11;

    public static void main(String[] args) {
        // 生成数据
        int[] dataList = new int[LENGTH];
        for (int index = 0; index < dataList.length; index++) {
            dataList[index] = (int) (Math.random() * LENGTH);
        }
        System.out.println(Arrays.toString(dataList));

        // 查找数据
        int index = seqSearch(dataList, VALUE);
        if (index != -1) {
            System.out.println("---- 找到数据索引值： " + index + " ----");
        } else {
            System.out.println("---- 未找到数据 ----");
        }
    }

    // 查找数据  返回下标
    private static int seqSearch(int[] dataList, int value) {
        for (int index = 0; index < dataList.length; index++) {
            if (value == dataList[index]) {
                return index;
            }
        }
        return -1;
    }
}
